package com.wushi.lenovo.asia5b.app_ui.pay.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.base.BaseSimpleFragment;
import com.mvp.utils.StringUtils;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_event.OrderPayEvent;
import com.wushi.lenovo.asia5b.app_model.credit.bean.TopupInfoBean;
import com.wushi.lenovo.asia5b.app_ui.pay.activity.PayActivity;
import com.wushi.lenovo.asia5b.app_ui.pay.activity.PayInsVesselActivity;
import com.wushi.lenovo.asia5b.app_ui.pay.view.PayPriceInfoView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyuncai on 2020/3/4.
 * 订单支付余额不足选择fpx/visa页面
 */
public class PayInsSelectFragment extends BaseSimpleFragment {
    @BindView(R.id.tv_pay_orderprice)
    TextView tv_pay_orderprice;
    @BindView(R.id.ll_pay_fpx)
    LinearLayout ll_pay_fpx;
    @BindView(R.id.ll_pay_visa)
    LinearLayout ll_pay_visa;
    @BindView(R.id.payprice)
    PayPriceInfoView payprice;
    private PayActivity payActivity;
    private TopupInfoBean topupInfoBean;

    @Override
    protected int getLayout() {
        return R.layout.fragment_payinsselect;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        payActivity = (PayActivity) mActivity;
        topupInfoBean = payActivity.topupInfoBean;
        initView();
        initEvent();
    }

    @Override
    protected void initView() {
        tv_pay_orderprice.setText(String.format("%s %s", getString(R.string.rm), StringUtils.formatNum2(topupInfoBean.getTotalAmount() + "")));
        payprice.setTopupInfoBean(topupInfoBean);
    }

    @OnClick({
            R.id.ll_pay_fpx,
            R.id.ll_pay_visa
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_pay_fpx: {
                Intent intent = new Intent(mActivity, PayInsVesselActivity.class);
                intent.putExtra("isFpx", true);
                intent.putExtra(CommonConstant.TopupInfoBean, topupInfoBean);
                startActivity(intent);
            }
            break;
            case R.id.ll_pay_visa: {
                Intent intent = new Intent(mActivity, PayInsVesselActivity.class);
                intent.putExtra("isFpx", false);
                intent.putExtra(CommonConstant.TopupInfoBean, topupInfoBean);
                startActivity(intent);
            }
            break;
            default:
                break;
        }
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void lazyFetchData() {

    }


    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onOrderPayEvent(OrderPayEvent event) {
        if (event != null) {
            if (event.resultModel.isSuccess()) {
                mActivity.finish();
            }
        }
    }
    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

}
